import {searchFiles} from '../src/input-check'
import {expect} from 'chai'
import * as util from '../src/util'
import {promises as fs} from 'fs'
import path from 'path'

describe('get ARCHIVE_PATH', () => {
    beforeEach(async () => {
        await fs.writeFile(path.join(__dirname, 'abc1'), 'Hello, this is a test file.');
        await fs.writeFile(path.join(__dirname, 'abc2'), 'Hello, this is a test file.');
        await fs.writeFile(path.join(__dirname, 'abc3'), 'Hello, this is a test file.');
        await fs.writeFile(path.join(__dirname, 'dd1'), 'Hello, this is a test file.');
        await fs.writeFile(path.join(__dirname, 'dd2'), 'Hello, this is a test file.');
        await fs.writeFile(path.join(__dirname, 'dd3'), 'Hello, this is a test file.');
        await fs.writeFile(path.join(__dirname, 'cc1'), 'Hello, this is a test file.');
        await fs.writeFile(path.join(__dirname, 'cc2'), 'Hello, this is a test file.');
        await fs.writeFile(path.join(__dirname, 'cc3'), 'Hello, this is a test file.');


    })
    afterEach(async () => {
        await fs.unlink(path.join(__dirname, 'abc1'))
        await fs.unlink(path.join(__dirname, 'abc2'))
        await fs.unlink(path.join(__dirname, 'abc3'))
        await fs.unlink(path.join(__dirname, 'dd1'))
        await fs.unlink(path.join(__dirname, 'dd2'))
        await fs.unlink(path.join(__dirname, 'dd3'))
        await fs.unlink(path.join(__dirname, 'cc1'))
        await fs.unlink(path.join(__dirname, 'cc2'))
        await fs.unlink(path.join(__dirname, 'cc3'))


    })

    it('get ARCHIVE_PATH has glob return all match abc* three', async () => {
        var paths =  searchFiles("abc*", __dirname)
        expect(paths).equal('abc1 abc2 abc3')
    })


    it('get ARCHIVE_PATH  return one', async () => {
        var paths =  searchFiles("abc1", __dirname)
        expect(paths).equal('abc1')
    })
    it('get ARCHIVE_PATH not match return origin path', async () => {
        var paths =  searchFiles("abc", __dirname)
        expect(paths).equal('abc')
    })
    it('get ARCHIVE_PATH has glob return all match dd* three', async () => {
        var paths =  searchFiles("dd*", __dirname)
        expect(paths).equal('dd1 dd2 dd3')
    })
    it('get ARCHIVE_PATH has glob and has two path return all match abc* dd* six', async () => {
        var paths =  searchFiles("abc* dd*", __dirname)
        expect(paths).equal('abc1 abc2 abc3 dd1 dd2 dd3')
    })
    it('get ARCHIVE_PATH has glob and has absolute path with sep return all match abc* absolute path', async () => {
        var paths =  searchFiles(`abc* ${(__dirname + util.SEP)}`, __dirname)
        expect(paths).contains(__dirname)
        expect(paths).contains('abc1 abc2 abc3')

    })
    it('get ARCHIVE_PATH has glob and has absolute path without sep return all match abc* absolute path', async () => {
        var paths =  searchFiles(`abc* ${__dirname}`, __dirname)
        expect(paths).contains(__dirname)
        expect(paths).contains('abc1 abc2 abc3')

    })
    it('get ARCHIVE_PATH has glob and has absolute path with * return all match abc* absolute path', async () => {
        let dir = __dirname+util.SEP
        var paths =  searchFiles(`abc* ${(dir + '*')}`, __dirname)
        expect(paths).contains(dir)
        expect(paths).contains('abc1 abc2 abc3')

    })
    it('get ARCHIVE_PATH has glob and has absolute path with cc* return all match abc* absolute path', async () => {
        const dir = __dirname+util.SEP
        var paths =  searchFiles(`abc* ${(dir + "cc*")}`, __dirname)
        expect(paths).contains(`${dir}cc1`)
        expect(paths).contains(`${dir}cc2`)
        expect(paths).contains(`${dir}cc3`)

        expect(paths).contains('abc1 abc2 abc3')

    })

})

